package com.example.movie.controller;

import com.example.movie.entity.TOnline;
import com.example.movie.entity.TUser;
import com.example.movie.service.TOnlineService;
import com.example.movie.service.TUserService;
import com.example.movie.vo.RestResult;
import com.example.movie.vo.RestResultBuilder;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * (TOnline)控制层
 *
 * @author makejava
 * @since 2021-04-02 16:51:12
 */
@RestController
@RequestMapping("/tOnline")
@CrossOrigin
public class TOnlineController {
    /**
     * 服务对象
     */
    @Resource
    private TOnlineService tOnlineService;
    @Resource
    private TUserService tUserService;

    /**
     * 通过主键查询单条数据
     *
     * @param tOnline 参数对象
     * @return 单条数据
     */
    @RequestMapping(value = "get", method = RequestMethod.GET)
    public RestResult selectOne(TOnline tOnline) {
        TOnline result = tOnlineService.selectById(tOnline.getOnlineId());
        if (result != null) {
            return new RestResultBuilder<>().success("查询成功", result);
        } else {
            return new RestResultBuilder<>().error("查询失败");
        }
    }

    /**
     * 新增一条数据
     *
     * @param tOnline 实体类
     * @return Response对象
     */
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public RestResult insert(TOnline tOnline) {
        int result = tOnlineService.insert(tOnline);
        if (result > 0) {
            return new RestResultBuilder<>().success("新增成功", tOnline);
        }
        return new RestResultBuilder<>().error("新增失败");
    }

    /**
     * 修改一条数据
     *
     * @param tOnline 实体类
     * @return Response对象
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public RestResult update(@RequestBody TOnline tOnline) {
        TOnline result = tOnlineService.update(tOnline);
        if (result != null) {
            return new RestResultBuilder<>().success("修改成功", result);
        }
        return new RestResultBuilder<>().error("修改失败");
    }

    /**
     * 删除一条数据
     *
     * @param tOnline 参数对象
     * @return Response对象
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public RestResult delete(@RequestBody TOnline tOnline) {
        int result = tOnlineService.deleteById(tOnline.getOnlineId());
        if (result > 0) {
            return new RestResultBuilder<>().success("删除成功", null);
        }
        return new RestResultBuilder<>().error("删除失败");
    }

    /**
     * 条件查询
     *
     * @return Response对象
     */
    @RequestMapping(value = "selectByModie", method = RequestMethod.GET)
    public RestResult selectAll(TOnline tOnline) {
        List<TOnline> tOnlines = tOnlineService.selectList(tOnline);
        List<TUser> tUsers = new ArrayList<TUser>();
        for (int i = 0; i < tOnlines.size() ; i++) {
            TUser tUser = tUserService.selectById(tOnlines.get(i).getUid());
            tUser.setOnline(tOnlines.get(i).getContent());
            tUser.setOnlineTime(tOnlines.get(i).getCreatime());
            tUsers.add(tUser);
        }
        if (tOnlines != null) {
            return new RestResultBuilder<>().success("查询成功", tUsers);
        }
        return new RestResultBuilder<>().error("查询失败");
    }

    /**
     * 分页查询
     *
     * @param start 偏移
     * @param limit 条数
     * @return Response对象
     */
    @RequestMapping(value = "selectPage", method = RequestMethod.GET)
    public RestResult selectPage(Integer start, Integer limit) {
        List<TOnline> tOnlines = tOnlineService.selectPage(start, limit);
        if (tOnlines != null) {
            return new RestResultBuilder<>().success("查询成功", tOnlines);
        }
        return new RestResultBuilder<>().error("查询失败");
    }



}
